package OThinker.servlet;

import OThinker.H3.Portal.webservices.impl.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Endpoint;
import java.io.File;
import java.io.PrintStream;
import java.util.Objects;

public class KumhosunnyStartServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private final static Logger LOGGER = LoggerFactory.getLogger(StartServlet.class);

    private Endpoint organizationWS;

    private Endpoint SSOWS;

    private Endpoint bpmWS;

    private Endpoint activityInfoWS;

    private Endpoint u8SampleWS;


    @Override
    public void init() {

        System.setProperty("h3bpm.point.flag", "portal");

        try {
            super.init();

            // 发布Webservice
            organizationWS = OrganizationImpl.startService();
            SSOWS = SSOServiceImpl.startService();
            bpmWS = BPMServiceImpl.startService();
            activityInfoWS = ActivityInfoServiceImpl.startService();
            u8SampleWS=U8SampleServiceImpl.startService();
        } catch (ServletException e) {
            LOGGER.error("启动失败", e);
            destroy();
            throw new RuntimeException(e);
        }
    }

    private void ignoreSysout() {
        try {
            String env = System.getenv("my_env");
            if (env == null) {
                env = System.getProperty("my_env");
            }
            if (Objects.equals(env, "dev")) {
                File tmpFile = new File(System.getProperty("user.dir") + "/tmp/h3bpm-sysout.log");
                if (!tmpFile.exists()) {
                    if (!tmpFile.getParentFile().exists()) {
                        tmpFile.getParentFile().mkdirs();
                    }
                    tmpFile.createNewFile();
                }
                PrintStream myPrintStream = new PrintStream(tmpFile);
                System.setOut(myPrintStream);
                System.setErr(myPrintStream);
            }
        } catch (Exception e) {
            // ignore
        }
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        this.doGet(request, response);
    }

    @Override
    public void destroy() {
        super.destroy();

        if (organizationWS != null) {
            try {
                LOGGER.info("stop ws organization start...");
                organizationWS.stop();
                LOGGER.info("stop ws organization end...");
            } catch (Exception e) {
                LOGGER.error("停止organizationWS异常", e);
            }
        }

        if (SSOWS != null) {
            try {
                LOGGER.info("stop ws sso start...");
                SSOWS.stop();
                LOGGER.info("stop ws sso end...");
            } catch (Exception e) {
                LOGGER.error("停止SSOWS异常", e);
            }
        }

        if (bpmWS != null) {
            try {
                LOGGER.info("stop ws bpm start...");
                bpmWS.stop();
                LOGGER.info("stop ws bpm end...");
            } catch (Exception e) {
                LOGGER.error("停止bpmWS异常", e);
            }
        }

        if (activityInfoWS != null) {
            try {
                LOGGER.info("stop ws activityInfo start...");
                activityInfoWS.stop();
                LOGGER.info("stop ws activityInfo end...");
            } catch (Exception e) {
                LOGGER.error("停止bpmWS异常", e);
            }
        }
    }

}
